﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Practices.Composite.Events;
using Jbe.TestSuite.TestDevices.Interface;
using Microsoft.Practices.Composite.Presentation.Events;
using Jbe.Composite.Extensions;
using Jbe.TestSuite.TestDevices.SignalVisualizer.Properties;

namespace Jbe.TestSuite.TestDevices.SignalVisualizer.Applications
{
    internal class VisualizerPresentationModel : PresentationModel<IVisualizerView>, IHeaderInfoProvider
    {
        private bool startTimeSet = false;
        private DateTime startTime;


        public VisualizerPresentationModel(IVisualizerView view, IEventAggregator eventAggregator)
            : base(view)
        {
            eventAggregator.GetEvent<SampleEvent>().Subscribe(SampleEventHandler, ThreadOption.UIThread);
        }


        public string HeaderInfo
        {
            get { return Resources.Visualizer; }
        }

        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/chart_curve.ico")); }
        }

        private void SampleEventHandler(SampleData e)
        {
            if (!startTimeSet)
            {
                startTime = e.TimeStamp;
                startTimeSet = true;
            }

            TimeSpan relativeTime = e.TimeStamp.Subtract(startTime);

            View.AddSample(e.Owner, e.Amplitude, relativeTime.TotalMilliseconds / 10);
        }
    }
}
